/**
 * 15-600: Java and J2EE
 * Instructor: Jeff Eppinger
 * Done By:	Mohammad al Khani
 * Date Dec 10, 2011
 */

package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.form.FormBeanFactory;

import model.BookmarkDAO;
import model.Model;
import model.UserDAO;
import dataBeans.Bookmark;
import dataBeans.User;
import formBean.AddBookmarksForm;

public class ManageAction extends Action {
	private FormBeanFactory<AddBookmarksForm> formBeanFactory = FormBeanFactory.getInstance(AddBookmarksForm.class);
	private UserDAO userDAO;
	private BookmarkDAO bookmarkDAO;
	
	public ManageAction(Model model){
		userDAO = model.getUserDAO();
		bookmarkDAO = model.getBookmarkDAO();
	}
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "manage.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
        try {
        	//TODO:check for errors
	        User user = (User)request.getSession().getAttribute("user");
	        if(user != null){
	        	bookmarkDAO = new BookmarkDAO();
	        	Bookmark[] userBookmarks = bookmarkDAO.getUserBookmarks(user.getUserId());
	        	for(int i = 0; i < userBookmarks.length; i++){
	        		System.out.println(userBookmarks[i].getUrl() + " " + userBookmarks[i].getComment());
	        	}
	        	request.getSession().setAttribute("userBookmarks", userBookmarks);
	        }else{
	        	System.err.println("user isn't there");
	        }
        }catch(Exception e){
        	e.printStackTrace();
        }
    	return "manageUserBookmarks.jsp";
    }
}
